package com.gress.shop.service.impl;

import com.google.common.collect.Lists;
import com.gress.shop.service.IAccessLogService;
import com.gress.shop.service.IChartClientService;
import com.gress.shop.service.inner.IMemberInternalService;
import com.gress.shop.util.HappyException;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ChartClientService implements IChartClientService {

    @Resource
    private IAccessLogService accessLogService;

    @Resource
    private IMemberInternalService memberInternalService;

    @Override
    public List<Integer> queryVisitNum(Long[] timeArrays, String email) {
        if (!StringUtils.isEmpty(email) && memberInternalService.queryByEmail(email) == null) {
            throw new HappyException(email + " 用户不存在");
        }
        if (ArrayUtils.isEmpty(timeArrays) || timeArrays.length == 1) {
            return Lists.newArrayList();
        }
        List<Long> list = Lists.newArrayList(timeArrays);
        List<Integer> backList = Lists.newArrayList();
        list.sort((o1, o2) -> (int) (o1 - o2));
        for (int i = 0; i < list.size() - 1; i++) {
            int size = accessLogService.queryByRegion(list.get(i), list.get(i + 1), email).size();
            backList.add(size);
        }
        return backList;
    }
}
